package com.app.activitys.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.app.entidades.Videos;
import com.example.applucianokikao.R;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;
import com.google.android.youtube.player.YouTubeThumbnailLoader.ErrorReason;


public class SelectedAdapterVideos extends ArrayAdapter implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener{
	
	private Map<View, YouTubeThumbnailLoader> mLoaders;
	private List<Videos> videos;
	public static final String API_KEY = "AIzaSyCjVH67tcOhOCbn3yH8OC9ogrs96boxWnc";
	private int cont=0;
	private Dialog di;
	private Context context;
	private List<String> chaves;
	private boolean flag=false;
	
	public SelectedAdapterVideos(Context context, final int layoutResourceId, List objects) {
		super(context, layoutResourceId, objects);
		mLoaders = new HashMap<View, YouTubeThumbnailLoader>();
		chaves = new ArrayList<String>();
		videos = objects;
		this.context = context;
		di = criarDialog();
		//di.show();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	  View view = convertView;
	  
	  Videos video = videos.get(position);	
	  String chave = retornaChave(video.getLink());
	  if(position>=0 && position<10){
		  System.out.println("Add: "+video.getTitulo()+" chave: "+chave);
		  chaves.add(chave);  
	  }
	  
	  if (view == null) {
		   LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   view = inflater.inflate(R.layout.selected_row_video, parent, false);
		   
		   YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.youtube_view);
		   thumbnail.setTag(chave);
		   thumbnail.initialize(API_KEY, this);
		   TextView nome = (TextView) view.findViewById(R.id.txt_nome_video);
		   nome.setText(video.getTitulo());
	  }else {
		  
		    YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.youtube_view);
		    YouTubeThumbnailLoader loader = mLoaders.get(thumbnail);
		    
		    if (loader == null) {
		    	thumbnail.setTag(chave);
		    } else {
		        loader.setVideo(chave);
		    }
		    
		    TextView nome = (TextView) view.findViewById(R.id.txt_nome_video);
		    nome.setText(video.getTitulo());
	 }
	  return view;
   }

	@Override
	public void onInitializationFailure(YouTubeThumbnailView arg0,
			YouTubeInitializationResult arg1) {
		
		System.out.println("Ocorreu algum problema.");
	}

	@Override
	public void onInitializationSuccess(YouTubeThumbnailView arg0,
			YouTubeThumbnailLoader arg1) {
		System.out.println("Iniciou com sucesso");
		String videoId = (String) arg0.getTag();
	    mLoaders.put(arg0, arg1);
	    arg1.setOnThumbnailLoadedListener(this);
	    arg1.setVideo(videoId);
	}

	public String retornaChave(String link){
		String saida = "";
		String[] v = link.split("v=");
		saida = v[1];
		return saida;
	}

	@Override
	public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
		
		System.out.println("Falha Thumbnail");
	}

	@Override
	public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
		System.out.println("Thumbnail Load");
		if(chaves.contains(arg1)){
			System.out.println("carregou: "+arg1+" cont++");
			cont++;	
		}
		
		if(cont==1 && !flag){
			System.out.println("fechou com: "+arg1);
			flag=true;
			//di.dismiss();
		}
		
	}
	
	public Dialog criarDialog(){
		Dialog di = new Dialog(context,R.style.Theme_Dialog_Translucent);
		di.setTitle("Carregando Videos");
        di.setCancelable(false);
        di.setContentView(R.layout.theme);
		return di;
	}
}
